var redis = require('redis');
var client = redis.createClient();

// 执行命令PFADD将用户添加到HyperLogLog键
function addVisit(date, user) {
    var key = 'visits:' + date;
    client.pfadd(key, user);
}

// 函数count显示指定日期的唯一身份访问次数：
function count(dates) {
    var keys = [];
    dates.forEach(function (date, index) {
        keys.push('visits:' + date);
    });
    client.pfcount(keys, function (err, reply) {
        console.log('Dates', dates.join(', '), 'had', reply, 'visits');
    });
}

// 函数aggregateDate将给定日期的访问合并给visits:2016-01-01
function aggregateDate(date) {
    var keys = ['visits:' + date];
    for (var i = 0; i < 24; i++) {
        keys.push('visits:' + date + 'T' + i);
    }
    client.pfmerge(keys, function (err, reply) {
        console.log('Aggregated date', date);
    });
}

var MAX_USERS = 200;
var TOTAL_VISITS = 1000;
for (var i = 0; i < TOTAL_VISITS; i++) {
    var username = 'user_' + Math.floor(1 + Math.random() * MAX_USERS);
    var hour = Math.floor(Math.random() * 24);
    addVisit('2016-01-01T' + hour, username);
}

count(['2016-01-01T0']);
count(['2016-01-01T5', '2016-01-01T6', '2016-01-01T7']);
aggregateDate('2016-01-01');
count(['2016-01-01']);
client.quit();

